package JDKdynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author:zimmor
 * @Date: 2021/3/25 10:10
 * @Description 通过匿名内部类的方式实现动态代理的过程
 * @version: 1.0.0
 */

public class DynamicProxyDemo2 {
    public static void main(String[] args) {
        //1.new一个被代理对象
        final Buyer1 buyer1 = new Buyer1();
        Subject proxy1 = (Subject) Proxy.newProxyInstance(buyer1.getClass().getClassLoader(), buyer1.getClass().getInterfaces(), new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("代理介入");
                return method.invoke(buyer1,args);
            }
        });
        proxy1.buybuybuy();
    }
}
